package com.yanqu.road.entity.activity.biaoju;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.utils.property.Property;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_biaoju_city", desc = "镖局商会城市数据")
public class CrossBiaoJuCityData extends DataObject {
    @FieldTypeAnnotation(select = true, update = true, isIndex = true)
    private int activityId;
    @FieldTypeAnnotation(update = true, isIndex = true, desc = "城市id")
    private int cityId;
    @FieldTypeAnnotation(update = true, isIndex = true, desc = "商会id")
    private String unionId;
    @FieldTypeAnnotation(select = true, update = true, desc = "商会分组")
    private int unionGroup;
    @FieldTypeAnnotation(desc = "积分")
    private long score;
    @FieldTypeAnnotation(desc = "本轮积分")
    private long roundScore;
    @FieldTypeAnnotation(desc = "投资时间")
    private long scoreChangeTime;
    @FieldTypeAnnotation(desc = "排名")
    private int rank;
    @FieldTypeAnnotation(desc = "上一轮排名")
    private int lastRank;
    @FieldTypeAnnotation(desc = "上一轮排名积分")
    private long lastRankScore;
    @FieldTypeAnnotation(desc = "每轮结算的排名")
    private Map<Integer, List<Long>> historyRankMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "每轮的玩家贡献")
    private Map<Integer, Map<Long, BiaoJuCityUserBuildInfo>> memberAddScoreMap = new ConcurrentHashMap<>();

    private long rankScore;
    private long rankTime;

    public CrossBiaoJuCityData() {
    }

    public CrossBiaoJuCityData(int activityId, int cityId, String unionId, int unionGroup) {
        this.activityId = activityId;
        this.cityId = cityId;
        this.unionId = unionId;
        this.unionGroup = unionGroup;
        setInsertOption();
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }

    public int getUnionGroup() {
        return unionGroup;
    }

    public void setUnionGroup(int unionGroup) {
        this.unionGroup = unionGroup;
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        this.score = score;
        setUpdateOption();
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        if (this.rank != rank) {
            this.rank = rank;
            setUpdateOption();
        }
    }

    public int getLastRank() {
        return lastRank;
    }

    public void setLastRank(int lastRank) {
        this.lastRank = lastRank;
        setUpdateOption();
    }

    public long getLastRankScore() {
        return lastRankScore;
    }

    public void setLastRankScore(long lastRankScore) {
        this.lastRankScore = lastRankScore;
        setUpdateOption();
    }

    public long getScoreChangeTime() {
        return scoreChangeTime;
    }

    public void setScoreChangeTime(long scoreChangeTime) {
        this.scoreChangeTime = scoreChangeTime;
    }

    public long getRankScore() {
        return rankScore;
    }

    public void setRankScore(long rankScore) {
        this.rankScore = rankScore;
    }

    public long getRankTime() {
        return rankTime;
    }

    public void setRankTime(long rankTime) {
        this.rankTime = rankTime;
    }

    public Map<Integer, List<Long>> getHistoryRankMap() {
        return historyRankMap;
    }

    public void setHistoryRankMap(Map<Integer, List<Long>> historyRankMap) {
        this.historyRankMap = historyRankMap;
    }

    public void updateHistoryRank(int round, long rank, long rankScore, long roundScore) {
        List<Long> list = new ArrayList<>();
        list.add(rank);
        list.add(rankScore);
        list.add(roundScore);
        historyRankMap.put(round, list);
        setUpdateOption();
    }

    public Map<Integer, Map<Long, BiaoJuCityUserBuildInfo>> getMemberAddScoreMap() {
        return memberAddScoreMap;
    }

    public void setMemberAddScoreMap(Map<Integer, Map<Long, BiaoJuCityUserBuildInfo>> memberAddScoreMap) {
        this.memberAddScoreMap = memberAddScoreMap;
        setUpdateOption();
    }

    public void memberAddScore(int round, long userId, long scoreAdd, long lastAddTime) {
        Map<Long, BiaoJuCityUserBuildInfo> map = memberAddScoreMap.get(round);
        if (map == null) {
            map = new ConcurrentHashMap<>();
            memberAddScoreMap.put(round, map);
        }
        BiaoJuCityUserBuildInfo info = map.get(userId);
        if (info == null) {
            info = new BiaoJuCityUserBuildInfo();
            map.put(userId, info);
        }
        info.setScoreAdd(info.getScoreAdd() + scoreAdd);
        info.setLastAddTime(lastAddTime);
        setUpdateOption();
    }

    public long getMemberAddScore(int round, long userId) {
        Map<Long, BiaoJuCityUserBuildInfo> map = memberAddScoreMap.get(round);
        if (map == null) {
            return 0;
        }
        BiaoJuCityUserBuildInfo info = map.get(userId);
        if (info == null) {
            return 0;
        }
        return info.getScoreAdd();
    }

    public long getHistoryRoundScore(int round) {
        List<Long> list = historyRankMap.get(round);
        if (list == null) {
            return 0;
        }
        return list.get(2);
    }

    public long getHistoryRankScore(int round) {
        List<Long> list = historyRankMap.get(round);
        if (list == null) {
            return 0;
        }
        return list.get(1);
    }

    public int getHistoryRoundRank(int round) {
        List<Long> list = historyRankMap.get(round);
        if (list == null) {
            return 0;
        }
        Long rank = list.get(0);
        return rank.intValue();
    }

    public long getRoundScore() {
        return roundScore;
    }

    public void setRoundScore(long roundScore) {
        this.roundScore = roundScore;
        setUpdateOption();
    }
}
